package com.hxzy.controller.vue.product;

import com.github.pagehelper.PageInfo;
import com.hxzy.common.constans.MarketingConstans;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.PageResp;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.MarketingThreadLocalUtil;
import com.hxzy.common.wrap.PageInfoUtil;
import com.hxzy.controller.vue.login.resp.AdminLoginResp;
import com.hxzy.controller.vue.product.req.*;
import com.hxzy.controller.vue.product.resp.ProductResp;
import com.hxzy.entity.Product;
import com.hxzy.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/9/15 15:57
 * @Version 1.0
 */
@RestController
@Api(tags = "后台-商品")
@RequestMapping(value = "/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<ProductResp>> search(@Valid ProductReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        //商家
        if(adminLoginResp.getPermissions().contains(MarketingConstans.MERCHANDISER)){
           req.setMerchantId(adminLoginResp.getId());
        }

        PageInfo<ProductResp> pageInfo=this.productService.search(req);
        return R.okHasData(PageInfoUtil.pageInfoToResp(pageInfo));
    }

    @ApiOperation(value = "商品上下架操作")
    @PostMapping(value = "/upordown")
    public R isgroundChange(@RequestBody @Valid IsGroudingChangeReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        Product product = this.productService.getById(req.getId());
        if(product==null){
            return R.build(AckCode.DATABASE_ERROR);
        }
         //不允许改别人的
        if(!adminLoginResp.getId().equals(product.getMerchantId())){
            return R.build(AckCode.PERMISSION_NOT_ACCESS_CATEGORY);
        }
        //参数一定要注意
        if(! (req.getIsgrounding().equals("0") || req.getIsgrounding().equals("1"))){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        Product changeProduct=new Product();
        changeProduct.setId(req.getId());
        changeProduct.setIsgrounding(req.getIsgrounding());

        boolean success = this.productService.updateById(changeProduct);
        return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "商品价格修改操作")
    @PostMapping(value = "/price/change")
    public R priceChange(@RequestBody @Valid PriceChangeReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        Product product = this.productService.getById(req.getId());
        if(product==null){
            return R.build(AckCode.DATABASE_ERROR);
        }
        //不允许改别人的
        if(!adminLoginResp.getId().equals(product.getMerchantId())){
            return R.build(AckCode.PERMISSION_NOT_ACCESS_CATEGORY);
        }

        if(req.getPrice()<0){
            return R.build(AckCode.PRODUCT_PRICE_ERROR);
        }

        Product changeProduct=new Product();
        changeProduct.setId(req.getId());
        changeProduct.setPrice(req.getPrice());

        boolean success = this.productService.updateById(changeProduct);
        return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }


    @ApiOperation(value = "商品库存修改操作")
    @PostMapping(value = "/stock/change")
    public R stockChange(@RequestBody @Valid StockChangeReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        Product product = this.productService.getById(req.getId());
        if(product==null){
            return R.build(AckCode.DATABASE_ERROR);
        }
        //不允许改别人的
        if(!adminLoginResp.getId().equals(product.getMerchantId())){
            return R.build(AckCode.PERMISSION_NOT_ACCESS_CATEGORY);
        }

        if(req.getNum()<-1){
            return R.build(AckCode.PRODUCT_STOCK_ERROR);
        }
        Product changeProduct=new Product();
        changeProduct.setId(req.getId());
        changeProduct.setNum(req.getNum());

        boolean success = this.productService.updateById(changeProduct);
        return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid ProductAddReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        if(adminLoginResp.getPermissions().contains(MarketingConstans.ADMIN)){
            return R.build(AckCode.PERMISSION_NOT_ACCESS);
        }

       boolean success=this.productService.add(req);
       return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping(value = "/{id}")
    public R<ProductResp> findById(@PathVariable(value = "id") Long id){
        if(Objects.isNull(id)){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }
        ProductResp resp= this.productService.findById(id);
        return R.okHasData(resp);
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Valid ProductEditReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        if(adminLoginResp.getPermissions().contains(MarketingConstans.ADMIN)){
            return R.build(AckCode.PERMISSION_NOT_ACCESS);
        }

        boolean success=this.productService.edit(req);
        return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "批量上下架商品")
    @PostMapping(value = "/batchUpOrDown")
    public R batchUpOrDown(@RequestBody @Valid BatchUpOrDownReq req){
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        if(adminLoginResp.getPermissions().contains(MarketingConstans.ADMIN)){
            return R.build(AckCode.PERMISSION_NOT_ACCESS);
        }

        boolean success=this.productService.batchUpOrDown(req);
        return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }
}
